package fhp.hlhj.giantfold.activity.red
import com.lzy.okgo.model.HttpParams
import fhp.hlhj.giantfold.BaseActivity
import fhp.hlhj.giantfold.Contents
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.customView.DayRedDialog
import fhp.hlhj.giantfold.customView.RuleDialog
import fhp.hlhj.giantfold.interfaces.IDayRed
import fhp.hlhj.giantfold.interfaces.IRedToCash
import fhp.hlhj.giantfold.interfaces.IUser
import fhp.hlhj.giantfold.javaBean.BaseBean
import fhp.hlhj.giantfold.javaBean.GetRedBean
import fhp.hlhj.giantfold.javaBean.UserInfoBean
import fhp.hlhj.giantfold.presenter.DayRedPresenter
import fhp.hlhj.giantfold.presenter.RedToCashPresenter
import fhp.hlhj.giantfold.presenter.UserPresenter
import fhp.hlhj.giantfold.utils.MyUtils
import kotlinx.android.synthetic.main.activity_day_red.*

class DayRed : BaseActivity(), IRedToCash ,IUser, IDayRed, DayRedDialog.OnOverShareListener {
    private lateinit var dayRedDialog:DayRedDialog
    private lateinit var userPresenter:UserPresenter
    private lateinit var presenter: DayRedPresenter

    private lateinit var redToCashPresenter:RedToCashPresenter
    override fun getContentId(): Int = R.layout.activity_day_red
    override fun initView() {
        userPresenter= UserPresenter(this)
        val params = HttpParams()
        params.put("user_id",Contents.userId)
        userPresenter.getUser(params)
        presenter= DayRedPresenter(this)
        redToCashPresenter= RedToCashPresenter(this)
        tvRedNum.text=Contents.userInfo?.tthb_not_trans_to_balance
    }
    override fun initListener() {
        btEarn.setOnClickListener {
            if (Contents.userInfo?.tthb_status.equals("2")){
                MyUtils.toast("今天已经不能领红包了哟")
            }else if (Contents.userInfo?.tthb_status.equals("0")){
                dayRedDialog= DayRedDialog(this,"s",this)
                dayRedDialog.show()
            }
            else{
                val p = HttpParams()
                p.put("user_id",Contents.userId)
                presenter.getDayRed(p)
            }
        }
        imgRule.setOnClickListener {
            RuleDialog("春庭聊纵望，楼台自相隐。\n" +
                    "窗梅落晚花，池竹开初荀。\n" +
                    "泉鸣知水急，云来觉山近。\n" +
                    "不愁花不飞，到畏花飞尽。",this).show()
        }
        btExit.setOnClickListener {
            finish()
        }
        tv2.setOnClickListener {
            val params = HttpParams()
            params.put("user_id",Contents.userId)
            params.put("amount",Contents.userInfo?.tthb_not_trans_to_balance)
            redToCashPresenter.redToCash(params)
        }
    }

    override fun getUser(userInfoBean: UserInfoBean) {
        loadingDialog.dismiss()
        if (userInfoBean.code==200){
            Contents.userInfo=userInfoBean.data
            tvRedNum.text=userInfoBean.data.tthb_not_trans_to_balance
        }

    }
    override fun showLoading() {
        loadingDialog.show()
    }
    override fun OnResp(baseBean: BaseBean) {
        loadingDialog.dismiss()
        MyUtils.toast(baseBean.msg)
        if (baseBean.code==200){
            MyUtils.toast("成功")
            val params = HttpParams()
            params.put("user_id",Contents.userId)
            userPresenter.getUser(params)

        }
    }
    /**
     * 领红包
     */
    override fun onResp(getRedBean: GetRedBean) {
        loadingDialog.dismiss()
        if (getRedBean.code==200){
            dayRedDialog= DayRedDialog(this,getRedBean.amount.toString(),this)
            dayRedDialog.show()
        }else{
            MyUtils.toast(getRedBean.msg)
        }
    }

    override fun over() {
        val params = HttpParams()
        params.put("user_id",Contents.userId)
        userPresenter.getUser(params)

    }
}
